use axum::Json;
use crate::controller;
use crate::http::server::error::Error;

pub trait IntoJsonResponseResult<T> {
    fn into_json_response_result(self) -> Result<Json<wanmesh_common::dto::Response<T>>, Error>;
}

impl<T> IntoJsonResponseResult<T> for Result<T, controller::Error> {
    fn into_json_response_result(self) -> Result<Json<wanmesh_common::dto::Response<T>>, Error> {
        match self {
            Ok(data) => Ok(Json(wanmesh_common::dto::Response::new_ok(data))),
            Err(e) => Err(Error::from(e)),
        }
    }
}